home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Binding / Server.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  5.3 KB  |  233 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Binding::Server - A server to accept incoming connections
  24.  
  25. =head1 SYNOPSIS
  26.  
  27. Creating a new server and accepting client connections
  28.  
  29.   use Net::DBus::Binding::Server;
  30.  
  31.   my $server = Net::DBus::Binding::Server->new(address => "unix:path=/path/to/socket");
  32.  
  33.   $server->connection_callback(\&new_connection);
  34.  
  35.   sub new_connection {
  36.       my $connection = shift;
  37.  
  38.       .. work with new connection...
  39.   }
  40.  
  41. Managing the server and new connections in an event loop
  42.  
  43.   my $reactor = Net::DBus::Binding::Reactor->new();
  44.  
  45.   $reactor->manage($server);
  46.   $reactor->run();
  47.  
  48.   sub new_connection {
  49.       my $connection = shift;
  50.  
  51.       $reactor->manage($connection);
  52.   }
  53.  
  54.  
  55. =head1 DESCRIPTION
  56.  
  57. A server for receiving connection from client programs.
  58. The methods defined on this module have a close
  59. correspondance to the dbus_server_XXX methods in the C API,
  60. so for further details on their behaviour, the C API documentation
  61. may be of use.
  62.  
  63. =head1 METHODS
  64.  
  65. =over 
  66.  
  67. =cut
  68.  
  69. package Net::DBus::Binding::Server;
  70.  
  71. use 5.006;
  72. use strict;
  73. use warnings;
  74.  
  75. use Net::DBus;
  76. use Net::DBus::Binding::Connection;
  77.  
  78. =item my $server = Net::DBus::Binding::Server->new(address => "unix:path=/path/to/socket");
  79.  
  80. Creates a new server binding it to the socket specified by the
  81. C<address> parameter.
  82.  
  83. =cut
  84.  
  85. sub new {
  86.     my $proto = shift;
  87.     my $class = ref($proto) || $proto;
  88.     my %params = @_;
  89.     my $self = {};
  90.  
  91.     $self->{address} = exists $params{address} ? $params{address} : die "address parameter is required";
  92.     $self->{server} = Net::DBus::Binding::Server::_open($self->{address});
  93.  
  94.     bless $self, $class;
  95.  
  96.     $self->{server}->_set_owner($self);
  97.  
  98.     $self->{_callback} = sub {
  99.     my $server = shift;
  100.     my $rawcon = shift;
  101.     my $con = Net::DBus::Binding::Connection->new(connection => $rawcon);
  102.  
  103.     if ($server->{connection_callback}) {
  104.         &{$server->{connection_callback}}($server, $con);
  105.     }
  106.     };
  107.  
  108.     return $self;
  109. }
  110.  
  111. =item $status = $server->is_connected();
  112.  
  113. Returns zero if the server has been disconnected,
  114. otherwise a positive value is returned.
  115.  
  116. =cut
  117.  
  118.  
  119. sub is_connected {
  120.     my $self = shift;
  121.     
  122.     return $self->{server}->dbus_server_get_is_connected();
  123. }
  124.  
  125. =item $server->disconnect()
  126.  
  127. Closes this server to the remote host. This method
  128. is called automatically during garbage collection (ie
  129. in the DESTROY method) if the programmer forgets to
  130. explicitly disconnect.
  131.  
  132. =cut
  133.  
  134. sub disconnect {
  135.     my $self = shift;
  136.     
  137.     return $self->{server}->dbus_server_disconnect();
  138. }
  139.  
  140.  
  141. =item $server->set_watch_callbacks(\&add_watch, \&remove_watch, \&toggle_watch);
  142.  
  143. Register a set of callbacks for adding, removing & updating 
  144. watches in the application's event loop. Each parameter
  145. should be a code reference, which on each invocation, will be
  146. supplied with two parameters, the server object and the
  147. watch object. If you are using a C<Net::DBus::Binding::Reactor> object
  148. as the application event loop, then the 'manage' method on
  149. that object will call this on your behalf.
  150.  
  151. =cut
  152.  
  153.  
  154. sub set_watch_callbacks {
  155.     my $self = shift;
  156.     my $add = shift;
  157.     my $remove = shift;
  158.     my $toggled = shift;
  159.  
  160.     $self->{add_watch} = $add;
  161.     $self->{remove_watch} = $remove;
  162.     $self->{toggled_watch} = $toggled;
  163.  
  164.     $self->{server}->_set_watch_callbacks();
  165. }
  166.  
  167. =item $server->set_timeout_callbacks(\&add_timeout, \&remove_timeout, \&toggle_timeout);
  168.  
  169. Register a set of callbacks for adding, removing & updating 
  170. timeouts in the application's event loop. Each parameter
  171. should be a code reference, which on each invocation, will be
  172. supplied with two parameters, the server object and the
  173. timeout object. If you are using a C<Net::DBus::Binding::Reactor> object
  174. as the application event loop, then the 'manage' method on
  175. that object will call this on your behalf.
  176.  
  177. =cut
  178.  
  179. sub set_timeout_callbacks {
  180.     my $self = shift;
  181.     my $add = shift;
  182.     my $remove = shift;
  183.     my $toggled = shift;
  184.  
  185.     $self->{add_timeout} = $add;
  186.     $self->{remove_timeout} = $remove;
  187.     $self->{toggled_timeout} = $toggled;
  188.  
  189.     $self->{server}->_set_timeout_callbacks();
  190. }
  191.  
  192. =item $server->set_connection_callback(\&handler)
  193.  
  194. Registers the handler to use for dealing with
  195. new incoming connections from clients. The code
  196. reference will be invoked each time a new client
  197. connects and supplied with a single parameter
  198. which is the C<Net::DBus::Binding::Connection> object representing
  199. the client.
  200.  
  201. =cut
  202.  
  203. sub set_connection_callback {
  204.     my $self = shift;
  205.     my $callback = shift;
  206.  
  207.     $self->{connection_callback} = $callback;
  208.  
  209.     $self->{server}->_set_connection_callback();
  210. }
  211.  
  212.  
  213. 1;
  214.  
  215.  
  216. =pod
  217.  
  218. =back
  219.  
  220. =head1 SEE ALSO
  221.  
  222. L<Net::DBus::Binding::Connection>, L<Net::DBus::Binding::Bus>, L<Net::DBus::Binding::Message::Signal>, L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>, L<Net::DBus::Binding::Message::Error>
  223.  
  224. =head1 AUTHOR
  225.  
  226. Daniel Berrange E<lt>dan@berrange.comE<gt>
  227.  
  228. =head1 COPYRIGHT
  229.  
  230. Copyright 2004 by Daniel Berrange
  231.  
  232. =cut
  233.